﻿using Dapper;
using Entity.Utils;
using EntityLibrary.Business.AccountProcess.Entity;
using EntityLibrary.Business.AccountProcess.Sql;
using EntityLibrary.DomainCheck;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace EntityLibrary.Business.AccountProcess
{
    public class AccountProcessService
    {
        readonly string connStr = Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["HISDATA"].ConnectionString);

        //登录(有patientID则返回，无则添加后返回patientID)
        public bool LoginWithPersonId(int CallNo, string CallName, ref Patient patient, ref string errorMessage)
        {
            if (default(KeyValuePair<string, string>).Equals(
                CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
                ))
            {
                errorMessage = "非法访问失败，请联系接口开发商！";
                return false;
            }


            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("Login.sql");

            if (!IDCardHelper.CheckIDCard(patient.IdCardNo) && !string.IsNullOrWhiteSpace(patient.IdCardNo))
                throw new InvalidOperationException("身份证格式错误");

            conn.Open();
            try
            {
                DataSet ds = new DataSet();
                SqlCommand sc = new SqlCommand(sqlStr, conn);
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@CallNo_In", Value = CallNo });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@CallName_In", Value = CallName });
                //sc.Parameters.Add(new SqlParameter() { ParameterName = "@CardType_In", Value = 6 });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@CardNo_In", Value = patient.CarNo });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@PatientName_In", Value = patient.PatientName });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@IdCardNo_In", Value = patient.IdCardNo });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@Sex_In", Value = patient.Sex });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@Birthday_In", Value = patient.Birthday });
                sc.Parameters.Add(new SqlParameter() { ParameterName = "@Address_In", Value = patient.Address });
                SqlDataReader sdr = sc.ExecuteReader();

                if (!sdr.HasRows) throw new InvalidOperationException("程序出错，请联系开发商！");
                sdr.Read();
                if ((int)sdr["Result"] != 1)
                {
                    errorMessage = (string)sdr["ResultText"];
                    return false;
                }
                patient.PatientId = (string)sdr["PatientId"];
                patient.Tel = Convert.ToString(sdr["Tel"]);
                patient.CreateDate = Convert.ToDateTime(sdr["CreateDate"]);
                patient.PatientName = Convert.ToString(sdr["PatientName"]);
                patient.Birthday = Convert.ToDateTime(sdr["Birthday"]);
                patient.IdCardNo = Convert.ToString(sdr["IdCardNo"]);
                patient.Sex = sdr["Sex"].ToString();
                return true;
            }
            finally
            {
                conn.Close();
            }
        }

        //用PatientId登录
        public Patient LoginWithPatientId(int CallNo, string CallName, string patientId)
        {
            if (default(KeyValuePair<string, string>).Equals(
                CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
                ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            var conn = new SqlConnection(connStr);

            string sqlStr = SQLFileHelper.GetSql("LoginById.sql");

            var sqlParam = new DynamicParameters();
            sqlParam.Add("@PatientId_In", patientId);
            var patient = conn.Query<Patient>(sqlStr, sqlParam).FirstOrDefault();
            conn.Close();
            return patient;
        }
    }
}
